package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

/*
 ------原生的数据库执行
	查询操作Raw
	增、删、改使用Exec
*/

func main() {
	//链接数据库
	db, err := gorm.Open("mysql", "root:123456@tcp(192.168.11.189:3306)/lztest?charset=utf8&parseTime=True&loc=Local")

	//链接失败，下面就不在执行
	if err != nil {
		panic(err)
	}

	//数据库使用结束释放资源
	defer db.Close()

	//跟踪数据库执行语句
	db.LogMode(true)

	var users []User
	//查询操作Raw
	db.Raw("select * from users where uid = ?", 1).Find(&users)
	fmt.Println(users)

	//增、删、改使用exec
	//db.Exec("insert into users(u_name,u_age) values (?,?)", "小明", 22)
	//db.Exec("update users set u_name = ? where uid = ?", "往往", 1)
	db.Exec("delete  from  users where uid = ?", 1)

}

type GetData struct {
	UName string
	UAge  int
	Add   string
	Desc  string
}

type User struct {
	UId   int `gorm:"primary_key;AUTO_INCREMENT"`
	UName string
	UAge  int
}

type UserInfo struct {
	InfoID int `gorm:"primary_key;AUTO_INCREMENT"`
	Add    string
	Desc   string
	User   User `gorm:"ForeignKey:Usid;AssociationForeignKey:UId"`
	Usid   int
}
